# -*- coding:utf-8 -*-

from basic_new import web_static, saveImg, write_data_excel
from lxml import etree
import os, shutil, time


class amazon_pic():

    def __init__(self, log_path):
        self.log_path = log_path
        self.img_path = log_path+'/img'
        self.home_url = "https://www.amazon.com"
        self.next_url = ""

    def get_all_elements(self, url):
        web = web_static(url)
        web_etree = etree.HTML(web)
        product_lst = []
        product_info = web_etree.xpath('//div[@class="s-item-container"]/div[3]/div[1]/a/@*')
        product_pics_url = web_etree.xpath('//div[@class="s-item-container"]/div[2]/div[1]//img/@src')
        self.next_url = web_etree.xpath('//div[@class="pagnHy"]/span[@class="pagnRA"]/a/@href')

        for i in range(len(product_info)):
            if i % 3 == 1:
                file_name = product_info[i].strip()
                file_link = product_info[i + 1].strip()
                product_lst.append([file_name, file_link])
        return product_lst, product_pics_url

    def gogogogo(self, short_url):
        product_lst = []
        pics_lst = []

        for i in range(3):
            if self.next_url == "":
                url = self.home_url + short_url
            else:
                url = self.home_url + self.next_url[0]
            print url
            tmp_product_lst, tmp_pics_lst = self.get_all_elements(url)
            product_lst.extend(tmp_product_lst)
            pics_lst.extend(tmp_pics_lst)
            print len(product_lst),len(pics_lst)
            time.sleep(2)
        latest_url = self.log_path + "/page_urls.txt"
        with open(latest_url,'a+') as f_url:
            f_url.write(self.next_url[0]+'\n')
        pics_all_path = saveImg(pics_lst, self.img_path)
        write_data_excel(product_lst, pics_all_path, self.log_path)


if __name__ == "__main__":

    #sorted by featured
    url2 = "/s/ref=lp_3733481_st?rh=n%3A1055398%2Cn%3A%211063498%2Cn%3A1063306%2Cn%3A1063318%2Cn%3A3733481&qid=1504349274&sort=featured-rank"
    log_path = "/root/log/"
    img_path = log_path +"/img"
    if os.path.exists(img_path):
        shutil.rmtree(img_path)
    os.makedirs(img_path)

    pic = amazon_pic(log_path)
    pic.gogogogo(url2)

